package pl.lodz.p.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pl.lodz.p.beans.utils.HibernateUtil;
import pl.lodz.p.beans.utils.Util;
import pl.lodz.p.model.Doctor;
import pl.lodz.p.model.User;
import pl.lodz.p.model.enums.AccountType;
import pl.lodz.p.services.DoctorService;
import pl.lodz.p.services.DoctorServiceImpl;
import pl.lodz.p.services.UserService;
import pl.lodz.p.services.UserServiceImpl;

public class DoctorManagedBean implements Serializable{

	private static final long serialVersionUID = 6178136185301841181L;

	private Doctor doctor;
	
	private DoctorService doctorService = new DoctorServiceImpl();
	
	private UserService userService = new UserServiceImpl();
	
	private static Logger log = Logger.getLogger(DoctorManagedBean.class);
	
	private boolean forMe = false;
	
	public DoctorManagedBean() {
		setDoctor(new Doctor());
	}
	public String save() {
		if (forMe){
			doctor.setUser(((LoginBean) Util.getBean("loginBean")).getLoginUser());
		}
		return doctorService.save(doctor).equals("saved") ? "showDoctor" : "";
	}
	
	public String reset(){
		doctor = new Doctor();
		return "";
	}
	
	public List<Doctor> getAllDoctor(){
		return doctorService.findAll();
	}
	
	public Doctor getDoctor() {
		return doctor;
	}
	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}
	
	public List<User> getOtherDoctorUsers(){
		LoginBean loginBean = (LoginBean) Util.getBean("loginBean");
		User u = loginBean.getLoginUser();
		Set<User> users = new HashSet<User>();
		if (!u.getAccType().equals(AccountType.PATIENT)){
			users.add(u);
		}
		users.addAll(userService.findDoctors());
		users.remove(u);
		List<User> res = new ArrayList<User>();
		res.add(u);
		res.addAll(users);
		return res;
	}
	
	public boolean isForMe() {
		return forMe;
	}
	public void setForMe(boolean forMe) {
		this.forMe = forMe;
	}
}
